package com.oa.hyperion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oa.hyperion.entity.Department;
import com.oa.hyperion.entity.Employee;
import com.oa.hyperion.entity.Post;
import com.oa.hyperion.repository.Base.DepartmentMapper;
import com.oa.hyperion.repository.Base.EmployeeMapper;
import com.oa.hyperion.repository.Base.PostMapper;
import com.oa.hyperion.service.EmployeeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private PostMapper postMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    /**
     * 传入需要找到的部门id
     * @param departId
     * @return
     */
    @Override
    public List<Employee> findAllEmployeeInSameDepartment(Integer departId) {
            //获取雇员信息
        List<Employee> list=employeeMapper.selectList(new QueryWrapper<>());

        List<Employee> li=new ArrayList<>();
            //设置岗位(岗位id和 雇员的 post 关联)
        for(Employee e:list){
            e.setPos(postMapper.selectById(e.getPost()));
        }

        //找到相同部门的雇员
        for(int i=0;i<list.size();i++){
                    //处理为分配岗位的员工
                if(list.get(i).getPos()==null){
                    continue;
                }
            if(list.get(i).getPos().getDepartment()==departId){
                li.add(list.get(i));
            }
        }
        return li;
    }



    //找到所有员工，加上岗位信息
    @Override
    public List<Employee> findEmployeeWithPost(){
        //获取雇员信息
        List<Employee> list=employeeMapper.selectList(new QueryWrapper<>());
        //获取岗位信息
        List<Post> posts=postMapper.selectList(new QueryWrapper<>());

        List<Employee> li=new ArrayList<>();

        //添加岗位pos到list中
        for(Employee e:list){
            for(Post p:posts){
                if(e.getPost()==p.getId()){
                    e.setPos(p);
                }
            }
        }
        return list;
    }


//显示没有任职 部门leader的员工
    @Override
    public List<Employee> findEmployeeIsNotLeader() {
        //获取雇员信息
        List<Employee> list = employeeMapper.selectList(new QueryWrapper<>());
        //获取部门的信息
        List<Department> delist = departmentMapper.selectList(new QueryWrapper<>());

        //用于保存雇员容器
        List<Employee> li = new ArrayList<>();

        for (Employee e : list) {
            for (Department d : delist) {
                if (e.getId() == d.getHeader()) {
                    li.add(e);
                    break;
                }
            }
        }
            //将获取到的员工数据全部删除，剩下的就是 不为leader的员工
        list.removeAll(li);

            return list;
    }

    @Override
    public int updateLeader(Integer leaderId) {

        //获取部门id
        QueryWrapper<Department> w1=new QueryWrapper<>();
        w1.eq("header",leaderId);
        Integer departId=departmentMapper.selectOne(w1).getId();

        //获取岗位是主管的postid
        QueryWrapper<Post> wrapper=new QueryWrapper<>();
        wrapper.eq("name","主管");
        wrapper.eq("department",departId);
        Integer postId=postMapper.selectOne(wrapper).getId();

            //修改该领导的职位
        Employee employee=new Employee();
            employee.setId(leaderId);
            employee.setPost(postId);

            return employeeMapper.updateById(employee);
    }

}
